package com.example.car

import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.car.adapter.CarAdapter
import com.example.car.databinding.ActivityCarListBinding
import com.example.car.vm.CarListViewModel
import com.example.common.view.view.BaseMvvmActivity
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/car/CarListActivity")
@AndroidEntryPoint
class CarListActivity : BaseMvvmActivity<ActivityCarListBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_car_list
    private val carListViewModel : CarListViewModel by viewModels()
    val carAdapter = CarAdapter()
    override fun initView() {
        super.initView()
        mDataBinding.add.setOnClickListener {
           ARouter.getInstance().build("/car/CarActivity").navigation()
        }
        mDataBinding.back.setOnClickListener {
            finish()
        }
        mDataBinding.rv.adapter = carAdapter
        mDataBinding.rv.layoutManager = LinearLayoutManager(this)

        carListViewModel.getCarList()
        carListViewModel.mSuccessLiveData.observe(this){
             ToastUtils.showLong("获取成功")
            carAdapter.setList(it.data)
            carAdapter.notifyDataSetChanged()
        }
        carListViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
        carAdapter.setOnItemClickListener { adapter, view, position ->
                ARouter.getInstance().build( "/car/CarPayActivity")
                .withString("carCode",carAdapter.getItem(position).plateNumber)
                .navigation()
        }
    }
    override fun onResume() {
        super.onResume()
        carListViewModel.getCarList()
    }
}